<?php
// +----------------------------------------------------------------------
// | 权限节点
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\model\Auth as AuthModel;

class Auth extends Base {

    private $AuthModel;

    public function _initialize() {
        parent::_initialize();
        $this->AuthModel = new AuthModel;
    }

    /**
     * 列表
     * @access public
     */
    public function index(){
        $this->isAuth('admin.auth.index');
        $parent_id = input('parent_id/d');
        //菜单开始
        $pathMaps = $this->AuthModel->select_path($parent_id);
        $editMenu = [
            ['name'=>'<i class="iconfont icon-cjsqm"></i> '.lang('lang_ui_add'),'url'=>url('admin/auth/add',['parent_id'=>$parent_id])]
        ];
        $this->assign('pathMaps',json_encode($pathMaps));
        $this->assign('editMenu',json_encode($editMenu));
        //菜单结束
        $where = ['parent_id'=>0];
        if($parent_id){
            $where =['parent_id'=>$parent_id];
        }
        $info = $this->AuthModel->select_lists($where);
        $page = $info->render();
        $this->assign('parent_id',$parent_id);
        $this->assign('lists',$info);
        $this->assign('page',$page);
        return $this->fetch(); 
    }
    
    /**
     * 添加权限节点
     * @access public
     */
    public function add() {
        $this->isAuth('admin.auth.add');
        if(request()->isAjax()){
            $data = [
                'parent_id' => input('post.parent_id/d'),
                'title'     => input('post.title/s'),
                'auth_code' => input('post.auth_code/s'),
            ];
            $validate = $this->validate($data,'Auth.add');
            if(true !== $validate){
                return json(['code'=>1,'message'=>$validate]);
            }
            $result  = $this->AuthModel->add_data($data);
            if(!$result){
                return json(['code'=>1,'message'=>lang('lang_message_error')]);
            }else{
                return json(['code'=>0,'message'=>lang('lang_message_success'),'data'=>['url'=>url('admin/auth/index',['parent_id'=>$data['parent_id']])]]);
            }
        }else{
            $parent_id = input('?parent_id')?input('parent_id/d'):0;
            //菜单开始
            $pathMaps = [
                ['name'=>'<i class="iconfont icon-arrowleft"></i> '.lang('lang_ui_back'),'url'=>url('admin/auth/index',['parent_id'=>$parent_id])]
            ];
            $this->assign('pathMaps',json_encode($pathMaps));
            $editMenu = [];
            $this->assign('editMenu',json_encode($editMenu));
            //菜单结束
            $info = $this->AuthModel->find_info($parent_id);
            $this->assign('info',$info);
            $this->assign('parent_id',$parent_id);
            return $this->fetch();
        }
    } 
    
    /**
     * 编辑权限节点
     * @access public
     */
    public function edit() {
        $this->isAuth('admin.auth.edit');
        if(request()->isAjax()){
            $data = [
                'id' => input('post.id/d'),
                'parent_id' => input('post.parent_id/d'),
                'title'     => input('post.title/s'),
                'auth_code' => input('post.auth_code/s'),
            ];
            $validate = $this->validate($data,'Auth.edit');
            if(true !== $validate){
                return json(['code'=>1,'message'=>$validate]);
            }
            $result  = $this->AuthModel->edit_data($data);
            if(!$result){
                return json(['code'=>1,'message'=>lang('lang_message_error')]);
            }else{
                return json(['code'=>0,'message'=>lang('lang_message_success'),'data'=>['url'=>url('admin/auth/index',['parent_id'=>$data['parent_id']])]]);
            }
        }else{
            $id        = input('id/d');
            $info = $this->AuthModel->find_info($id);
            //菜单开始
            $pathMaps = [
                ['name'=>'<i class="iconfont icon-arrowleft"></i> '.lang('lang_ui_back'),'url'=>url('admin/auth/index',['parent_id'=>$info['parent_id']])]
            ];
            $this->assign('pathMaps',json_encode($pathMaps));
            $editMenu = [];
            $this->assign('editMenu',json_encode($editMenu));
            //菜单结束
            $parent_info = $this->AuthModel->find_info($info['parent_id']);
            $this->assign('parent_info',$parent_info);
            $this->assign('info',$info);
            return $this->fetch();
        }
    }
    
    /**
     * 删除
     * @access public
     */
    public function delete() {
        $this->isAuth('admin.auth.delete');
        $id   = input('id/d');
        $info = $this->AuthModel->find_info(['parent_id'=>$id]);
        if($info){
            return json(['code'=>1,'message'=>lang('lang_auth_delete')]);
        }
        $result  = $this->AuthModel->delete_data($id);
        if(!$result){
            return json(['code'=>1,'message'=>lang('lang_message_error')]);
        }else{
            return json(['code'=>0,'message'=>lang('lang_message_success')]);
        }
    }

    /**
     * 读取权限列表返回JSON数据
     * @return [type] [description]
     */
    public function ids(){
        $id   = input('id/d');
        $info = $this->AuthModel->select_Lists_ids($id);
        return json($info);
    }
}